package com.yimi.appstore.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * 自定义横向滑动页面ViewPager类
 * 
 * @author Tank
 * 
 */
public class MyViewPager {
	private ViewPager awesomePager;
	private AwesomePagerAdapter awesomeAdapter;

	/**
	 * 构造函数
	 * 
	 * @param context
	 * @param id
	 * @param mListViews
	 */
	public MyViewPager(Context context, int id, List<View> mListViews) {
		awesomePager = (ViewPager) ((Activity) context).findViewById(id);
		if (awesomeAdapter == null) {
			awesomeAdapter = new AwesomePagerAdapter(context, mListViews);
		}
		awesomePager.setAdapter(awesomeAdapter);
	}

	/**
	 * 设配器
	 * 
	 * @author Administrator
	 * 
	 */
	class AwesomePagerAdapter extends PagerAdapter {
		private List<View> mListViews;

		public AwesomePagerAdapter(Context context, List<View> views) {
			this.mListViews = views;
		}

		public int getCount() {
			return mListViews.size();
		}

		/**
		 * 从指定的position创建page
		 * 
		 * @param container
		 *            ViewPager容器
		 * @param position
		 *            The page position to be instantiated.
		 * @return 返回指定position的page，这里不需要是一个view，也可以是其他的视图容器.
		 */
		public Object instantiateItem(View collection, int position) {

			((ViewPager) collection).addView(mListViews.get(position), 0);
			return mListViews.get(position);
		}

		public void destroyItem(View collection, int position, Object view) {
			((ViewPager) collection).removeView(mListViews.get(position));
		}

		public boolean isViewFromObject(View view, Object object) {
			return view == (View) (object);
		}

		public void finishUpdate(View arg0) {
		}

		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		public Parcelable saveState() {
			return null;
		}

		public void startUpdate(View arg0) {
		}

		@Override
		public int getItemPosition(Object object) {
			return PagerAdapter.POSITION_NONE;
		}
	}

	/**
	 * 获取ViewPager实例
	 * 
	 * @return
	 */
	public ViewPager getAwesomePager() {
		return awesomePager;
	}

	public AwesomePagerAdapter getAwesomePagerAdapter() {
		return awesomeAdapter;
	}
}
